package com.demo.thread.threadpool;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 */
public class ArrayBlockQueueTest {
    private  static ThreadPoolExecutor thread ;
    private static AtomicInteger count;
    public static void main(String[] args)  {
        final ArrayBlockingQueue  queue = new ArrayBlockingQueue(10000);
        for(int i = 0;i<10000;i++){
            try {
                queue.put(":"+i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(":size;"+queue.size());

        thread = new ThreadPoolExecutor(20, 25, 1000, TimeUnit.DAYS,
                new ArrayBlockingQueue<>(30), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                try {
                    executor.getQueue().put(r);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        count = new AtomicInteger();

        for(int i = 0;i<10000;i++) {
           try{
                thread.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println("数据->" + queue.take());
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        while(thread.getActiveCount() >=1){}
        thread.shutdown();

    }
}
